//
//  WBStatusDetailView.m
//  WeiBo
//
//  Created by wbs on 17/2/14.
//  Copyright © 2017年 xiaomaolv. All rights reserved.
//

#import "WBStatusDetailView.h"

#import "WBStatusOriginalView.h"
#import "WBStatusRetweetedView.h"

#import "WBStatusDetailFrame.h"
#import "WBTextDiyView.h"
#import "WBStatus.h"

@interface WBStatusDetailView () <UIGestureRecognizerDelegate>

@end

@implementation WBStatusDetailView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self createView];
        
    }
    return self;
}

- (void)createView
{
    WBStatusOriginalView *originalView = [[WBStatusOriginalView alloc] init];
    [self addSubview:originalView];
    _originalView = originalView;
    
    WBStatusRetweetedView *retweetedView = [[WBStatusRetweetedView alloc] init];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(retweetedTap:)];
    tapGes.delegate = self;
    [retweetedView addGestureRecognizer:tapGes];
    [self addSubview:retweetedView];
    _retweetedView = retweetedView;
    
}

- (void)setDetailFrame:(WBStatusDetailFrame *)detailFrame
{
    _detailFrame = detailFrame;
    
    self.frame = detailFrame.selfFrame;
    
    self.originalView.originalFrame = detailFrame.originalFrame;
    self.retweetedView.retweetedFrame = detailFrame.retweetedFrame;
}

- (void)retweetedTap:(UITapGestureRecognizer *)tapGes
{
    if ([self.delegate respondsToSelector:@selector(statusDetailViewDidTapReweetedStatus:)]) {
        [self.delegate statusDetailViewDidTapReweetedStatus:self];
    }
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint point = [touch locationInView:self.retweetedView.retweetTextLabel];
    __block BOOL isContain = NO;
    [self.retweetedView.retweetTextLabel.links enumerateObjectsUsingBlock:^(WBVisialLink * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (isContain) *stop = YES;
        [obj.rects enumerateObjectsUsingBlock:^(NSValue * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGRect rect = obj.CGRectValue;
            if (CGRectContainsPoint(rect, point)) {
                isContain = YES;
                *stop = YES;
            }
        }];
    }];
    if (isContain) {
        return NO;
    } else {
        return YES;
    }
}

@end
